﻿using System;
using System.Windows.Forms;
using Daq331m;

namespace DIO_Demo
{
    public partial class Form1 : Form
    {
        private readonly Daq331M _daq331;
        private DataGridViewRow _levelRow;
        private DataGridViewRow _countRow;
        private DataGridViewRow _frequencyRow;

        public Form1()
        {
            InitializeComponent();

            _daq331 = new Daq331M();

            _levelRow = new DataGridViewRow();
            _levelRow.CreateCells(this.dataGridView1);
            _levelRow.Cells[0].Value = "电平";
            dataGridView1.Rows.Insert(0, _levelRow);

            _countRow = new DataGridViewRow();
            _countRow.CreateCells(this.dataGridView1);
            _countRow.Cells[0].Value = "次数";
            dataGridView1.Rows.Insert(1, _countRow);

            _frequencyRow = new DataGridViewRow();
            _frequencyRow.CreateCells(this.dataGridView1);
            _frequencyRow.Cells[0].Value = "频率(Hz)";
            dataGridView1.Rows.Insert(2, _frequencyRow);
        }

        private void ConnectPushButton_Click(object sender, EventArgs e)
        {
            _daq331.DeleteDevice();
            _daq331.CreateDevice();

            _daq331.UseTcpClientBackend("192.168.1.100", 8000);

            // 初始化设备
            Console.WriteLine(!_daq331.InitializeDevice() ? @"DAQ331M设备初始化失败" : @"DAQ331M设备初始化成功");
        }

        private void set_do_button_Click(object sender, EventArgs e)
        {
            var do1 = checkBox1.Checked ? 1 : 0;
            var do2 = checkBox2.Checked ? 1 : 0;
            var do3 = checkBox3.Checked ? 1 : 0;
            var do4 = checkBox4.Checked ? 1 : 0;
            var do5 = checkBox5.Checked ? 1 : 0;
            var do6 = checkBox6.Checked ? 1 : 0;

            var doState = ((byte)(do6) << 5) | ((byte)(do5) << 4) | ((byte)(do4) << 3) | ((byte)(do3) << 2) |
                          ((byte)(do2) << 1) | ((byte)(do1));

            Console.WriteLine(!_daq331.ConfigGpo((uint)doState) ? @"DAQ331M配置DO失败" : @"DAQ331M配置DO成功");
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            var state = radioButton1.Checked;
            if (!state) return;
            const int dryContact = 1;
            Console.WriteLine(!_daq331.ConfigGpi(dryContact)
                ? @"DAQ331M配置DI失败"
                : @"DAQ331M配置DI成功");
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            var state = radioButton2.Checked;
            if (!state) return;
            const int wetContact = 2;
            Console.WriteLine(!_daq331.ConfigGpi(wetContact)
                ? @"DAQ331M配置DI失败"
                : @"DAQ331M配置DI成功");
        }

        private void read_di_button_Click(object sender, EventArgs e)
        {
            var gpiState = _daq331.ReadGpi();

            for (var i = 0; i < 6; i++)
            {
                var state = "高电平";
                if (gpiState[i] == 0)
                {
                    state = "低电平";
                }

                _levelRow.Cells[i + 1].Value = state;
            }

            for (var i = 0; i < 6; i++)
            {
                var index = 6 + i * 4;
                var gpiCount = BitConverter.ToUInt32(gpiState, index);
                var state = gpiCount.ToString();
                _countRow.Cells[i + 1].Value = state;
            }

            for (var i = 0; i < 6; i++)
            {
                var index = 30 + i * 4;
                var gpiFrequency = BitConverter.ToUInt32(gpiState, index);
                var state = gpiFrequency.ToString();
                _frequencyRow.Cells[i + 1].Value = state;
            }
        }

        private void clear_button_Click(object sender, EventArgs e)
        {
            var di1 = checkBox12.Checked ? 1 : 0;
            var di2 = checkBox11.Checked ? 1 : 0;
            var di3 = checkBox10.Checked ? 1 : 0;
            var di4 = checkBox9.Checked ? 1 : 0;
            var di5 = checkBox8.Checked ? 1 : 0;
            var di6 = checkBox7.Checked ? 1 : 0;

            var diState = ((byte)(di6) << 5) | ((byte)(di5) << 4) | ((byte)(di4) << 3) | ((byte)(di3) << 2) |
                          ((byte)(di2) << 1) | ((byte)(di1));

            Console.WriteLine(!_daq331.ClearGpiCount((uint)diState) ? @"DAQ331M配置DI失败" : @"DAQ331M配置DI成功");
        }
    }
}